import QtQuick 1.1
import com.nokia.symbian 1.1
import "SettingsStorage.js" as Settings

Page{
    id: root

    property variant imageList
    property bool busy: false

    property string backgroundText: qsTr("Background color: ")
    property string marginText: qsTr("Use margins: ")
    property string scaleText: qsTr("Each image is size: ")
    property string aspectText: qsTr("Keep aspect ratio: ")

    property string colorTextWhite: qsTr("white")
    property string colorTextBlack: qsTr("Black")
    property string colorTextRed: qsTr("red")
    property string colorTextYellow: qsTr("yellow")
    property string colorTextSteelblue: qsTr("steelblue")
    property string colorTextLightGray: qsTr("lightgray")

    states:[
        State{
            name: "busy"
            when: busy
            PropertyChanges {
                target: mainRect
                opacity: 0
            }
            PropertyChanges {
                target: busyIndicator
                opacity: 1
            }
        },
        State{
            name: "ready"
            when: !busy
            PropertyChanges {
                target: mainRect
                opacity: 1
            }
            PropertyChanges {
                target: busyIndicator
                opacity: 0
            }
        }
    ]

    BusyIndicator{
        id: busyIndicator
        anchors.centerIn: parent
        running: true
        Behavior on opacity { NumberAnimation{ duration: 100; easing.type: Easing.OutBack } }
    }

    Rectangle{
        id: mainRect
        anchors.fill: parent
        color: "transparent"
        Behavior on opacity { NumberAnimation{ duration: 100; easing.type: Easing.OutBack } }

        Column{

            anchors.left: parent.left
            anchors.right: parent.right

            spacing: 7
            Text{
                anchors.horizontalCenter: parent.horizontalCenter
                color: "white"


                id: filenameHelp
                wrapMode: Text.WordWrap                
                text: qsTr("Set filename for image.")
                font.bold: true
            }
            Text{
                anchors.right: parent.right
                anchors.left: parent.left
                color: "white"
                wrapMode: Text.WordWrap
                text: qsTr("Created image is in JPG-format")
            }
            Text{
                anchors.right: parent.right
                anchors.left: parent.left
                color: "white"
                wrapMode: Text.WordWrap
                text: qsTr("File extension not needed, it will be inserted if not found.")
            }

            TextField{
                id: filenameField

                text: "picturePlugin.freeFilename"
                anchors.right: parent.right
                anchors.rightMargin: 0
                anchors.left: parent.left
                anchors.leftMargin: 0
                anchors.margins: 2

                Image{
                    anchors{
                        top: parent.top
                        right: parent.right
                        margins: 2
                    }
                    visible: filenameField.text
                    height: parent.height
                    width: parent.height
                    source: "qrc:/icons/clear.png"

                    MouseArea{
                        anchors.fill: parent
                        onClicked: {
                            filenameField.text = ""
                            filenameField.forceActiveFocus()
                        }
                    }
                }
            }

            Text{
                anchors.horizontalCenter: parent.horizontalCenter
                color: "white"
                id: settingsHelp
                wrapMode: Text.WordWrap
                text: qsTr("Using settings")
                font.bold: true
            }

            Text{
                id: scaleHelpText
                anchors{
                    right: parent.right
                    left: parent.left
                }
                wrapMode: Text.WordWrap
                color: "white"
            }
            Text{
                id: aspectHelpText
                anchors{
                    right: parent.right
                    left: parent.left
                }
                wrapMode: Text.WordWrap
                color: "white"
            }
            Text{
                id: aspectNoteText
                anchors{
                    right: parent.right
                    left: parent.left
                }
                wrapMode: Text.WordWrap
                color: "white"
                text: qsTr("Trying to keep aspect ratio, scaled image might be smaller than wanted size")
                visible: false
            }

            Text{
                id: marginHelpText
                anchors{
                    right: parent.right
                    left: parent.left
                }
                wrapMode: Text.WordWrap
                color: "white"
            }
            Text{
                id: backgroundHelpText
                anchors{
                    right: parent.right
                    left: parent.left
                }
                wrapMode: Text.WordWrap
                color: "white"
            }

            Button{
                id: createButton
                width: parent.width
                height: font.pixelSize*2
                text: qsTr("Create image")
                onClicked: {
                    if(!filenameField.text){
                        errorDialog.text = qsTr("Enter filename")
                        errorDialog.open();
                    }else{
                        busy = true
                        createImage();
                    }
                }
            }

        }
    }

    onVisibleChanged: {
        if(visible){
            filenameField.text = picturePlugin.freeFilename
            scaleHelpText.text = scaleText+picturePlugin.imageSizeWithResolutionAsString(Settings.getSetting("imagesize"))

            if(picturePlugin.aspectRatio===1){
                aspectHelpText.text = aspectText + qsTr("Yes")
                aspectNoteText.visible = true
            }else{
                aspectHelpText.text = aspectText + qsTr("No")
                aspectNoteText.visible = false
            }

            if(picturePlugin.margin !== 0){
                marginHelpText.text = marginText + qsTr("Yes")
            }else{
                marginHelpText.text = marginText + qsTr("No")
            }

            var color = Settings.getSetting("background")
            switch(color){
            case "white":{
                backgroundHelpText.text = backgroundText+colorTextWhite
            }break;
            case "Black":{
                backgroundHelpText.text = backgroundText+colorTextBlack
            }break;
            case "red":{
                backgroundHelpText.text = backgroundText+colorTextRed
            }break;
            case "yellow":{
                backgroundHelpText.text = backgroundText+colorTextYellow
            }break;
            case "steelblue":{
                backgroundHelpText.text = backgroundText+colorTextSteelblue
            }break;
            case "lightgray":{
                backgroundHelpText.text = backgroundText+colorTextLightGray
            }break;

            default:break;
            }
        }
    }

    function createImage(){
        var imglist = [];
        for(var i=0;i<root.imageList.count;i++){
            console.log("IMAGE: "+root.imageList.get(i)+" - "+root.imageList.get(i).url);
            imglist[i] = root.imageList.get(i).url;
        }

        var color = Settings.getSetting("background");
        if(!color){
            color = "white";
        }

        var imgsize = Settings.getSetting("imagesize");
        if(!imgsize){
            imgsize = picturePlugin.VGA
        }

        var margin = Settings.getSetting("margin");
        if(!margin){
            margin = 6
        }

        var aspect = Settings.getSetting("aspectratio");
        if(!aspect){
            aspect = 1
        }

        picturePlugin.createImage(imgsize,imglist,filenameField.text,color,margin,aspect);
    }
}
